home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / Imaging / PIL / CurImagePlugin.py < prev    next >
Encoding:
Text File  |  2000-06-23  |  1.8 KB  |  89 lines

  1. #
  2. # The Python Imaging Library.
  3. # $Id: CurImagePlugin.py,v 1.1.1.1 1998/08/18 13:07:52 sjoerd Exp $
  4. #
  5. # Windows Cursor support for PIL
  6. #
  7. # notes:
  8. #    uses BmpImagePlugin.py to read the bitmap data.
  9. #
  10. # history:
  11. #    96-05-27 fl    Created
  12. #
  13. # Copyright (c) Secret Labs AB 1997.
  14. # Copyright (c) Fredrik Lundh 1996.
  15. #
  16. # See the README file for information on usage and redistribution.
  17. #
  18.  
  19.  
  20. __version__ = "0.1"
  21.  
  22. import string
  23.  
  24. import Image, BmpImagePlugin
  25.  
  26.  
  27. #
  28. # --------------------------------------------------------------------
  29.  
  30. def i16(c):
  31.     return ord(c[0]) + (ord(c[1])<<8)
  32.  
  33. def i32(c):
  34.     return ord(c[0]) + (ord(c[1])<<8) + (ord(c[2])<<16) + (ord(c[3])<<24)
  35.  
  36.  
  37. def _accept(prefix):
  38.     return prefix[:4] == "\0\0\2\0"
  39.  
  40.  
  41. class CurImageFile(BmpImagePlugin.BmpImageFile):
  42.  
  43.     format = "CUR"
  44.     format_description = "Windows Cursor"
  45.  
  46.     def _open(self):
  47.  
  48.     offset = self.fp.tell()
  49.  
  50.     # check magic
  51.     s = self.fp.read(6)
  52.     if not _accept(s):
  53.         raise SyntaxError, "not an CUR file"
  54.  
  55.     # pick the largest cursor in the file
  56.     m = ""
  57.     for i in range(i16(s[4:])):
  58.         s = self.fp.read(16)
  59.         if not m:
  60.         m = s
  61.         elif ord(s[0]) > ord(m[0]) and ord(s[1]) > ord(m[1]):
  62.         m = s
  63.         #print "width", ord(s[0])
  64.         #print "height", ord(s[1])
  65.         #print "colors", ord(s[2])
  66.         #print "reserved", ord(s[3])
  67.         #print "hotspot x", i16(s[4:])
  68.         #print "hotspot y", i16(s[6:])
  69.         #print "bytes", i32(s[8:])
  70.         #print "offset", i32(s[12:])
  71.  
  72.     # load as bitmap
  73.     self._bitmap(i32(m[12:]) + offset)
  74.  
  75.     # patch up the bitmap height
  76.     self.size = self.size[0], self.size[1]/2
  77.     d, e, o, a = self.tile[0]
  78.     self.tile[0] = d, (0,0)+self.size, o, a
  79.  
  80.     return
  81.     
  82.  
  83. #
  84. # --------------------------------------------------------------------
  85.  
  86. Image.register_open("CUR", CurImageFile, _accept)
  87.  
  88. Image.register_extension("CUR", ".cur")
  89.